/**
 * Created by Aliaksandr Lazerka on 10/29/14.
 */

function onresize() {
    var documentWidth, documentHeight, documentRel, div = document.getElementById('div');
    div.style.width = '0px';
    div.style.height = '0px';
    div.style.top = '0px';
    div.style.left = '0px';

    documentWidth = $(document).width();
    documentHeight = $(document).height();

    documentRel = documentWidth / documentHeight;

    fullScreenProportionalElem(documentRel, documentWidth, documentHeight, div, 5400, 3768);
    resizeFont(documentRel, div, 5400, 3768, 100);
}

function fullScreenProportionalElem(documentRel, documentWidth, documentHeight, elem, width, height) {
    var rel = width / height;

    if (documentRel < rel) {
        elem.style.width = documentWidth + 'px';
        elem.style.height = Math.round(documentWidth / rel) + 'px';
        elem.style.top = Math.round(documentHeight / 2 - elem.offsetHeight / 2) + 'px';
        elem.style.left = '0px';
    }
    else if (documentRel > rel) {
        elem.style.width = Math.round(documentHeight * rel) + 'px';
        elem.style.height = documentHeight + 'px';
        elem.style.top = '0px';
        elem.style.left = Math.round(documentWidth / 2 - elem.offsetWidth / 2) + 'px';
    }
    else {
        elem.style.width = documentWidth + 'px';
        elem.style.height = documentHeight + 'px';
        elem.style.top = '0px';
        elem.style.left = '0px';
    }
}

function resizeFont(documentRel, elem, width, height, size) {
    var rel = width / height, n = 14062;

    if (documentRel < rel) elem.style.fontSize = height * size / n + 'vw';
    else if (documentRel > rel) elem.style.fontSize = width * size / n + 'vh';
}